package com.util.sms;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 阿里云发送短信
 * @createTime 2020/11/3
 */
@Component
public class AliyunSmsApiUtil {

    @Autowired
    AliyunSmsProperties aliyunSmsProperties;

    public AliyunSmsApiUtil() {
    }

    public AliyunSmsApiUtil(AliyunSmsProperties aliyunSmsProperties) {
        this.aliyunSmsProperties = aliyunSmsProperties;
    }


    /**
     * 多个手机发送不同内容
     *
     * @param phoneNumberJson   手机号 ["15900000000","13500000000"]
     * @param signNameJson      签名 ["阿里云","阿里巴巴"]
     * @param templateCode      模板编码
     * @param templateParamJson 模板变量 [{"name":"TemplateParamJson"},{"name":"TemplateParamJson"}]
     * @return
     * @throws ClientException
     */
    public CommonResponse sendBatchSms(String phoneNumberJson, String signNameJson, String templateCode,
                                       String templateParamJson) throws ClientException {
        CommonResponse response = null;
        // 初始化acsClient,暂不支持region化
        IAcsClient client = buildAcsClient();

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendBatchSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumberJson", phoneNumberJson);
        request.putQueryParameter("SignNameJson", signNameJson);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParamJson", templateParamJson);
        try {
            response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return response;
    }

    /**
     * 多个手机发送相同内容
     *
     * @param mobiles           支持对多个手机号码
     * @param signName          签名
     * @param templateCode      模板编码
     * @param templateParamJson 模板变量，json格式
     * @param outId             业务序列号，短信回执消息中将此值带回给调用者
     * @return
     * @throws ClientException
     */
    public CommonResponse sendSms(String mobiles, String signName, String templateCode,
                                   String templateParamJson, String outId) throws ClientException {
        CommonResponse response =null;
        //初始化acsClient,暂不支持region化
        IAcsClient client = buildAcsClient();

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", mobiles);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", templateParamJson);
        request.putQueryParameter("OutId", outId);
        try {
            response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return response;
    }

    /**
     * 初始化acsClient,暂不支持region化
     *
     * @return
     * @throws ClientException
     */
    private IAcsClient buildAcsClient() throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",  aliyunSmsProperties.getAccessKeyId(), aliyunSmsProperties.getAccessKeySecret());
        return new DefaultAcsClient(profile);
    }


}
